/*
 * Created by JFormDesigner on Mon Jun 24 15:44:43 CST 2024
 */

package scct.zhurui.ui;

import scct.zhurui.dao.UserDao;
import scct.zhurui.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author zr155
 */
public class LoginFrom extends JFrame {
    public LoginFrom() {
        initComponents();
    }

    private void btnlogin(ActionEvent e) {
        System.out.println("老登不要点啦");
//        获取文本框的输入的用户名密码
        String name=txtName.getText();
        String pass=txtPass.getText();
        System.out.println(name+","+pass);
//        name.matches("正则表达式")返回true表示成功，返回false表示验证失败
      if(!name.matches("^\\w{3,10}$"))
      {
          //           swing 的消息的对话框
        JOptionPane.showMessageDialog(null,"你输入的用户名不合法，请重新输入");
        return;
      }
      else if (!pass.matches("^\\w{3,10}$"))
      {
          JOptionPane.showMessageDialog(null,"你输入的密码不合格，请重新输入");
          return;
      }
        UserDao userDao = new UserDao();
       User user=userDao.login(name, pass);
        if (user==null){
            JOptionPane.showMessageDialog(this,"用户名密码错误，请检查登录");
            return;
        }else {
            JOptionPane.showMessageDialog(this, "登录成功");
           FromMain fromMain=new FromMain();
            fromMain .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fromMain.setSize(800,600);
            fromMain.setLocationRelativeTo(null);
            fromMain.setVisible(true);
            this.dispose();
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhur
        txtName = new JTextField();
        label1 = new JLabel();
        btnlogin = new JButton();
        txtPass = new JTextField();
        btnReg = new JButton();
        label2 = new JLabel();
        label5 = new JLabel();
        this2 = new JFrame();
        textField3 = new JTextField();
        label3 = new JLabel();
        button3 = new JButton();
        textField4 = new JTextField();
        button4 = new JButton();
        label4 = new JLabel();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(txtName);
        txtName.setBounds(110, 85, 160, 30);

        //---- label1 ----
        label1.setText("\u767b\u5f55");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(label1);
        label1.setBounds(165, 40, 95, 30);

        //---- btnlogin ----
        btnlogin.setText("\u767b\u5f55");
        btnlogin.addActionListener(e -> btnlogin(e));
        contentPane.add(btnlogin);
        btnlogin.setBounds(100, 170, 78, 30);
        contentPane.add(txtPass);
        txtPass.setBounds(110, 125, 160, 30);

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        contentPane.add(btnReg);
        btnReg.setBounds(200, 170, 78, 30);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        contentPane.add(label2);
        label2.setBounds(70, 85, 65, 30);

        //---- label5 ----
        label5.setText("\u5bc6\u7801");
        contentPane.add(label5);
        label5.setBounds(80, 125, 65, 30);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 305);
        setLocationRelativeTo(getOwner());

        //======== this2 ========
        {
            Container this2ContentPane = this2.getContentPane();
            this2ContentPane.setLayout(null);
            this2ContentPane.add(textField3);
            textField3.setBounds(110, 85, 160, 30);

            //---- label3 ----
            label3.setText("\u767b\u5f55");
            this2ContentPane.add(label3);
            label3.setBounds(150, 35, 65, 30);

            //---- button3 ----
            button3.setText("\u767b\u5f55");
            this2ContentPane.add(button3);
            button3.setBounds(new Rectangle(new Point(85, 170), button3.getPreferredSize()));
            this2ContentPane.add(textField4);
            textField4.setBounds(110, 125, 160, 30);

            //---- button4 ----
            button4.setText("\u767b\u5f55");
            this2ContentPane.add(button4);
            button4.setBounds(205, 175, 78, 30);

            //---- label4 ----
            label4.setText("text");
            this2ContentPane.add(label4);
            label4.setBounds(50, 70, label4.getPreferredSize().width, 2);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < this2ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = this2ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = this2ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                this2ContentPane.setMinimumSize(preferredSize);
                this2ContentPane.setPreferredSize(preferredSize);
            }
            this2.pack();
            this2.setLocationRelativeTo(this2.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }



    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhur
    private JTextField txtName;
    private JLabel label1;
    private JButton btnlogin;
    private JTextField txtPass;
    private JButton btnReg;
    private JLabel label2;
    private JLabel label5;
    private JFrame this2;
    private JTextField textField3;
    private JLabel label3;
    private JButton button3;
    private JTextField textField4;
    private JButton button4;
    private JLabel label4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
